package cn.itsource.wechat.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.wechat.pojo.dto.BinderDto;
import cn.itsource.wechat.pojo.info.WeChatProperties;
import cn.itsource.wechat.service.IWxChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 微信控制器
 */
@Controller
@Api(tags = "微信三方登录管理", description = "登录操作接口")
@RequestMapping("/wechat")
public class WeChatController {

    @Autowired
    private WeChatProperties weChatProperties;

    @Autowired
    private IWxChatService wxChatService;

    /**
     * employee登录
     */
    @GetMapping("/test")
    @ApiOperation(value = "测试")
    public void test() {

        System.out.println(weChatProperties);
        System.out.println(weChatProperties.getAppId());

        //WeChatProperties(tokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code, authorizationCodeUrl=https://open.weixin.qq.com/connect/qrconnect?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_login&state=%s#wechat_redirect, userinfoUrl=https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s, refreshTokenUrl=https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%s&grant_type=refresh_token&refresh_token=%s, appId=wxd853562a0548a7d0, appSecrect=4a5d5615f93f24bdba2ba8534642dbb6, redirectUri=http://bugtracker.itsource.cn/wechat/callback)
    }

    /**
     * 跳转到扫码页面
     */
    @GetMapping("/login")
    @ApiOperation(value = "跳转到扫码页面")
    public String login() {
        //跳转扫码页面
        return wxChatService.login();
    }
    /**
     * 扫码之后自动回调的接口,携带授权码code值
     */
    @GetMapping("/callback")
    @ApiOperation(value = "扫码之后自动回调的接口")
    public String callback(String code) {
        //三部曲入口
        return wxChatService.callback(code);
    }

    @PostMapping("/binder")
    @ApiOperation(value = "扫码之后自动回调的接口")
    @ResponseBody //注意:这里加,上面两个不加
    public AjaxResult binder(@RequestBody BinderDto binderDto) {
        //三部曲入口
        return wxChatService.binder(binderDto);
    }


    public static void main(String[] args) {
        String s =  "code=%s&state=%s";

        String code = "0000";
        String state = "ok";

        String format = String.format(s, code, state);
        System.out.println(format);//code=0000&state=ok
    }
}
